package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    //统计报表服务
    /**     * 获得运营统计数据    
     * Map数据格式：    
     *      todayNewMember -> number     *
     *      totalMember -> number     *
     *      thisWeekNewMember -> number     *
     *      thisMonthNewMember -> number     *
     *      todayOrderNumber -> number     *
     *      todayVisitsNumber -> number     *
     *      thisWeekOrderNumber -> number     *
     *      thisWeekVisitsNumber -> number     *
     *      thisMonthOrderNumber -> number     *
     *      thisMonthVisitsNumber -> number     *
     *      hotSetmeal -> List<Setmeal>     */
    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
      //通过工具类获取当前日期，并且转换为字符串格式
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获取本周星期一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月第一天
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //查询今日新增加的会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //查询本周新增加的会员
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //查询本月新增会员
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数        
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数        
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数        
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);
        //热门套餐,取预约人数前四的套餐       
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        //将查询出的数据封装到map集合中
        Map<String,Object> data =new HashMap<>();
        data.put("reportDate",today);
        data.put("todayNewMember",todayNewMember);
        data.put("totalMember",totalMember);
        data.put("thisWeekNewMember",thisWeekNewMember);
        data.put("thisMonthNewMember",thisMonthNewMember);
        data.put("todayOrderNumber",todayOrderNumber);
        data.put("thisWeekOrderNumber",thisWeekOrderNumber);
        data.put("thisMonthOrderNumber",thisMonthOrderNumber);
        data.put("todayVisitsNumber",todayVisitsNumber);
        data.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        data.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        data.put("hotSetmeal",hotSetmeal);
        return data;
    }
}
